import { useModel } from 'umi';
import { currentUser as queryCurrentUser } from '@/services/user';

export const fetchUserInfo = async () => {
  const { data } = await queryCurrentUser();
  return data
    ? {
        name: data.username,
        avatar: data.avatar,
        userid: data.uid,
        email: data.email,
        phone: data.phonenumber,
        access: data.access,
        roles: data.roles.map((item: any) => item.group_id),
        // signature: data.signature,
        // title: data.title,
        // group: data.group,
        // tags: data.tags,
        // notifyCount: data.notifyCount,
        // unreadCount: data.unreadCount,
        // country: data.country,
        // geographic: data.geographic,
        // address: data.address,
      }
    : undefined;
};

export default () => {
  const { setInitialState } = useModel('@@initialState');
  const _fetchUserInfo = async () => {
    const currentUser = await fetchUserInfo();
    await setInitialState((s) => ({
      ...s,
      currentUser,
    }));
  };

  return { fetchUserInfo: _fetchUserInfo };
};
